<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as  echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.width;
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData);
    },
    setOptions(data) {
      this.chart.setOption({
        title: {
          text: data.title,
          left: 'left',
          padding: [
            10, // 上
            10, // 右
            10, // 下
            10 // 左
          ],
          textStyle: { color: '#ffffff', fontSize: '14', fontWeight: 'bolder' }
        },
        tooltip: {
          trigger: 'item',
          textStyle: { color: 'rgb(255,255,255)' }
        },
        legend: {
          icon: 'circle',
          orient: 'vertical',
          top: '50px',
          left: '70%',
          textStyle: { color: '#ffffff' }
        },
        series: [
          {
            name: '数量',
            type: 'pie',
            radius: ['40%', '70%'],
            center: ['36%', '50%'],
            avoidLabelOverlap: false,
            label: {
              show: false
            },
            emphasis: {
              /*   label: {
                  show: false,
                  fontSize: "24",
                  fontWeight: "bold",
                }, */
              focus: 'self'
            },
            labelLine: {
              show: true
            },
            data: data.data
          }
        ]
      })
    }
  }
}
</script>
